/**
 * 
 */
package com.nttuyen.httpclient.test;

import java.io.BufferedReader;
import java.io.InputStreamReader;


import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.junit.Before;
import org.junit.Test;

import vn.support.util.StringUtil;


/**
 * @author nttuyen
 *
 */
public class HttpClientDemo {
	private static final String URL = "http://live.nttuyen.com/support/winapp/index.php";
	private static final String module = "livesupport";
	private String action = "";
	private String sessionId = "";
	
	private HttpClient httpClient = new DefaultHttpClient();
	private HttpPost post;
	private BufferedReader input;
	private StringBuilder response = new StringBuilder();
	
	@Before
	public void before() {
		this.login();
	}
	public void login() {
		System.out.println("Login");
		this.response = new StringBuilder();
		this.post = new HttpPost(URL + "?_ca=login&username=nttuyen&password=" + StringUtil.md5("adminpass"));
		try {
			HttpResponse res = this.httpClient.execute(this.post);
			HttpEntity entity = res.getEntity();
			if(entity != null) {
				this.input = new BufferedReader(new InputStreamReader(entity.getContent()));
				String line = null;
				while ((line = this.input.readLine()) != null) {
					this.response.append(line);
					this.response.append("\n");
					line = line.trim();
					if(line.indexOf("<sessionid>") != -1) {
						line = line.replace("<sessionid><![CDATA[", "");
						line = line.replace("]]></sessionid>", "");
						this.sessionId = line;
					}
				}
			}
			System.out.println("Response: " + this.response.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Test
	public void testFetchVisitors() {
		System.out.println("TestFetchVisitors");
		this.response = new StringBuilder();
		this.action = "fetchvisitors";
		this.post = new HttpPost(URL + "?_m=" + module + "&_a=" + this.action + "&sessionid=" + this.sessionId);
		System.out.println(this.post.getURI());
		try {
			HttpResponse res = this.httpClient.execute(this.post);
			HttpEntity entity = res.getEntity();
			if(entity != null) {
				this.input = new BufferedReader(new InputStreamReader(entity.getContent()));
				String line = null;
				while((line = this.input.readLine()) != null) {
					this.response.append(line);
					this.response.append("\n");
				}
			}
			System.out.println("Response: " + this.response.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Test
	public void testProactiveforcedchat() {
		System.out.println("testProactiveforcedchat");
		this.response = new StringBuilder();
		this.action = "proactiveforcedchat";
		String visitorId = "fdf1c2ec77a3b147c359f7ffd3401116";
		this.post = new HttpPost(URL + "?_m=" + module + "&_a=" + this.action + "&sessionid=" + this.sessionId + "&visitorsessionid=" + visitorId);
		System.out.println(this.post.getURI());
		try {
			HttpResponse res = this.httpClient.execute(this.post);
			HttpEntity entity = res.getEntity();
			if(entity != null) {
				this.input = new BufferedReader(new InputStreamReader(entity.getContent()));
				String line = null;
				while((line = this.input.readLine()) != null) {
					this.response.append(line);
					this.response.append("\n");
				}
			}
			System.out.println("Response: " + this.response.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
